<?PHP

require_once WACT_ROOT . 'db/db.inc.php';
require_once WACT_ROOT .'util/arraydataset.inc.php';
require_once APP_ROOT.'model/common/site.php';


class LogoBannersModel {

	function LogoBannersModel() {
	}

    // gets the banner name by type
	function getBannerName($type) {
        switch ($type) {
          case 0:
            return "Site Logo 255*86";
            break;
          case 1:
            return "Top banner 468*60";
            break;
          case 2:
            return "Left banner (max width 130)";
            break;
          case 3:
            return "Inside article banner 300*250";
            break;
          case 4:
            return "Right banner (max width 160)";
            break;
          case 11:
            return "Row 1: Bottom Ad Box 1";
            break;
          case 12:
            return "Row 1: Bottom Ad Box 2";
            break;
          case 13:
            return "Row 1: Bottom Ad Box 3";
            break;
          case 21:
            return "Row 2: Bottom Ad Box 1";
            break;
          case 22:
            return "Row 2: Bottom Ad Box 2";
            break;
          case 23:
            return "Row 2: Bottom Ad Box 3";
            break;
          case 31:
            return "Row 2: Bottom Ad Box 1";
            break;
          case 32:
            return "Row 2: Bottom Ad Box 2";
            break;
          case 33:
            return "Row 2: Bottom Ad Box 3";
            break;
        }
	}

	function &getBanner($stype) {
		$site_id = Site::getSiteID();
		if($stype=='TOP') $type=1;
		else if($stype=='LEFT') $type=2;
		else if($stype=='ART') $type=3;
		else if($stype=='RIGHT') $type=4;
		else if($stype=='ROW1ADBOX1') $type=11;
		else if($stype=='ROW1ADBOX2') $type=12;
		else if($stype=='ROW1ADBOX3') $type=13;
        else if($stype=='ROW2ADBOX1') $type=21;
		else if($stype=='ROW2ADBOX2') $type=22;
		else if($stype=='ROW2ADBOX3') $type=23;
        else if($stype=='ROW3ADBOX1') $type=31;
		else if($stype=='ROW3ADBOX2') $type=32;
		else if($stype=='ROW3ADBOX3') $type=33;

		$query="select * from sport_logobanner
		where type=$type
		and site_id = ".DBC::makeLiteral($site_id);
		$result=DBC::NewRecordSet($query);
		$result->reset();
		if($result->next()) {
			$source = $result->get('source');
			$target_url = $result->get('target_url');
			$alt = $result->get('alt');
			if(!$source) {   // custom image
				//$id=$result->get('id');
				//$query2="select path,filename_local from sport_files where file_id=$id";
				//$result2=DBC::NewRecordSet($query2);
				//$result2->reset();
				//if($result2->next()){
					//$banner=$result2->get('path').'/'.$result2->get('filename_local');
					//$bannerarray=array($stype.'banner_img',$banner);
				//}
				$banner = $result->get('url');
				if ($type>10) $bannertype = 'ADBOX'.$type.'banner_img';
				else $bannertype = $stype.'banner_img';
                $bannerarray = array($bannertype,$banner,$target_url,$alt);
			}
			else{   // script
				$banner = $result->get('script');
                if ($type>10) $bannertype = 'ADBOX'.$type.'banner_scr';
				else $bannertype = $stype.'banner_scr';
				$bannerarray = array($bannertype,$banner,$target_url,$alt);
			}
		}
		return $bannerarray;

	}

	function &getLogo() {
        $site_id = Site::getSiteID();
		//$query="select path,filename_local from sport_files a,sport_logobanner b where a.file_id=b.file_id and b.type=1";
		$sql = "select * from sport_logobanner
		where `type`=0
		and site_id = ".DBC::makeLiteral($site_id);
		$result=DBC::NewRecordSet($sql);
		$result->reset();
		if($result->next()){
			//$logo=$result->get('path').'/'.$result->get('filename_local');
			$logo = $result->get('url');
		}
		return $logo;
	}


    // 0 - logo, 1 - top banner, 2 - left, 3 - inside
	function &getInfo($type) {
		$site_id = Site::getSiteID();
		$sql = 'select * from sport_logobanner
		where `type`='.DBC::makeLiteral($type).'
		and site_id = '.DBC::makeLiteral($site_id);
		$rec =& DBC::FindRecord( $sql );
		return $rec;
	}

	function isExistInfo($type)  {
		$rec =& LogoBannersModel::getInfo($type);
		return !is_null( $rec );
	}

	function &getDefaultInfo($type) {
		$rec =& LogoBannersModel::getInfo($type);
		if (is_null( $rec )) {
            $data =& new DataSpace();
    		$data->set('source',0);
        	$data->set('script','');
        	$data->set('url','');
        	$data->set('type',$type);
        	$data->set('target_url',HOST);
        	$data->set('alt','');
        	$data->set('site_id',0);
        	return $data;
		}
		return $rec;
	}



    function &updateInfo($type, $source,$script,$url,$target_url,$alt) {
        $site_id = Site::getSiteID();
    	$isExist = LogoBannersModel::isExistInfo($type);
    	$data =& new DataSpace();
    	$data->set('source',$source);
        $data->set('script',$script);
        $data->set('url',$url);
        $data->set('`type`',$type);
        $data->set('target_url',$target_url);
        $data->set('alt',$alt);
        $data->set('site_id',$site_id);

        $Record =& DBC::NewRecord($data);
        if ($isExist)
        	$Record->update('sport_logobanner', array('source', 'script', 'url','target_url','alt','site_id'),
            "`type` = ". DBC::makeLiteral($type). " and "."site_id = ".DBC::makeLiteral($site_id));
        else $Record->insertId('sport_logobanner',
            array('`type`','source', 'script', 'url','target_url','alt','site_id'), 'id');
    }


}
?>
